package com.company.cloud.uba.modules.app.service.impl;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.util.IdUtil;
import com.company.cloud.crm.modules.api.request.Contact2UserAddRequest;
import com.company.cloud.crm.modules.api.service.CrmRemoteService;
import com.company.common.server.config.mq.models.MQSysOpLogModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.company.cloud.uba.modules.api.request.SysLogAddBatchRequest;
import com.company.cloud.uba.modules.api.request.SysLogAddRequest;
import com.company.cloud.uba.modules.api.request.UserBrowseAddBatchRequest;
import com.company.cloud.uba.modules.api.request.UserBrowseAddRequest;
import com.company.cloud.uba.modules.api.request.UserBrowseObjLogAddBatchRequest;
import com.company.cloud.uba.modules.api.request.UserBrowseObjLogAddRequest;
import com.company.cloud.uba.modules.api.request.UserLoginLogBatchRequest;
import com.company.cloud.uba.modules.api.request.UserLoginLogRequest;
import com.company.cloud.uba.modules.app.service.UserOpLogService;
import com.company.cloud.uba.modules.base.entity.SysOpLog;
import com.company.cloud.uba.modules.base.entity.UserBrowseLog;
import com.company.cloud.uba.modules.base.entity.UserBrowseObjLog;
import com.company.cloud.uba.modules.base.entity.UserLoginLog;
import com.company.cloud.uba.modules.base.service.SysOpLogService;
import com.company.cloud.uba.modules.base.service.UserBrowseLogService;
import com.company.cloud.uba.modules.base.service.UserBrowseObjLogService;
import com.company.cloud.uba.modules.base.service.UserLoginLogService;

import io.swagger.annotations.ApiModelProperty;

@Service
public class UserOpLogServiceImpl implements UserOpLogService {

	@Autowired
	private SysOpLogService sysOpLogService;
	@Autowired
	private UserBrowseLogService userBrowseLogService;
	@Autowired
	private UserLoginLogService userLoginLogService;
	@Autowired
	private UserBrowseObjLogService userBrowseObjLogService;
	@Autowired
	private CrmRemoteService crmRemoteService;

	@Override
	public void saveSysLogOne(SysLogAddRequest params) {
		SysOpLog entity = new SysOpLog();
//		entity.setcompanyNo
//		entity.setuserId
//		entity.setuserName
//		entity.setopName
//		entity.setopMethod
//		entity.setopParams
//		entity.setopUa
//		entity.setopStartDate
//		entity.setopUseTime
//		entity.setip
//		entity.setcdt
//		entity.setcman
//		entity.set
//		entity.set
//		entity.set
	}

	@Override
	public void saveSysLogOne(MQSysOpLogModel params) {

	}

	@Override
	public void saveSysLogBatch(SysLogAddBatchRequest params) {

	}

	@Override
	public void saveUserLoginLogOne(UserLoginLogRequest params) {
		UserLoginLog entity = new UserLoginLog();
		entity.setCompanyNo(params.getCompanyNo());
		entity.setUserId(params.getUserId());
		entity.setThirdUserId(params.getThirdUserId());
		entity.setUserName(params.getUserName());
		entity.setToken(params.getToken());
		entity.setLoginType(params.getLoginType());
		entity.setLoginIp(params.getLoginIp());
		entity.setClientType(params.getClientType());
		entity.setMobileNo(params.getMobileNo());
		entity.setLongitude(params.getLongitude());
		entity.setLatitude(params.getLatitude());
		entity.setLoginDate(params.getLoginDate());
		userLoginLogService.save(entity);
	}

	@Override
	public void saveUserLoginLogBatch(UserLoginLogBatchRequest params) {
		List<UserLoginLog> userLoginList = new ArrayList<UserLoginLog>();
		params.getItems().forEach(item -> {
			UserLoginLog entity = new UserLoginLog();
			entity.setCompanyNo(item.getCompanyNo());
			entity.setUserId(item.getUserId());
			entity.setThirdUserId(item.getThirdUserId());
			entity.setUserName(item.getUserName());
			entity.setToken(item.getToken());
			entity.setLoginType(item.getLoginType());
			entity.setLoginIp(item.getLoginIp());
			entity.setClientType(item.getClientType());
			entity.setMobileNo(item.getMobileNo());
			entity.setLongitude(item.getLongitude());
			entity.setLatitude(item.getLatitude());
			entity.setLoginDate(item.getLoginDate());
			userLoginList.add(entity);
		});

		if (userLoginList.size() > 0) {
			userLoginLogService.saveBatch(userLoginList);
		}

	}

	@Override
	public void saveUserBrowseBatch(UserBrowseAddBatchRequest params) {
		List<UserBrowseLog> userBrowseLogList=new ArrayList<UserBrowseLog>();
		params.getItems().forEach(item->{
			UserBrowseLog entity=new UserBrowseLog();
			entity.setId(IdUtil.getSnowflake(10,10).nextIdStr());
			entity.setUserToken(item.getUserToken());
			entity.setAnonyToken(item.getAnonymousToken());
			entity.setReferralCode(item.getReferralCode());
			entity.setMethodUrl(item.getMethodUrl());
			entity.setMethodType(item.getMethodType());
			entity.setMethodParam(item.getMethodParam());
			entity.setClientUa(item.getClientUa());
			entity.setCman(-1);
			entity.setCdt(LocalDateTime.now());
			entity.setCip(item.getIp());
		});
		if (userBrowseLogList.size() > 0) {
			userBrowseLogService.saveBatch(userBrowseLogList);
		}
	}

	@Override
	public void saveUserBrowseOne(UserBrowseAddRequest params) {
		UserBrowseLog entity=new UserBrowseLog();
		entity.setId(IdUtil.getSnowflake(10,10).nextIdStr());
		entity.setUserToken(params.getUserToken());
		entity.setAnonyToken(params.getAnonymousToken());
		entity.setReferralCode(params.getReferralCode());
		entity.setMethodUrl(params.getMethodUrl());
		entity.setMethodType(params.getMethodType());
		entity.setMethodParam(params.getMethodParam());
		entity.setClientUa(params.getClientUa());
		entity.setCman(-1);
		entity.setCdt(LocalDateTime.now());
		entity.setCip(params.getIp());
		userBrowseLogService.save(entity);
	}

	@Override
	public void saveUserBrowseObjLogBatch(UserBrowseObjLogAddBatchRequest params) {
		List<UserBrowseObjLog> userBrowseObjList=new ArrayList<UserBrowseObjLog>();
		params.getItems().forEach(item->{
			UserBrowseObjLog entity=new UserBrowseObjLog();
			entity.setCompanyNo(item.getCompanyNo());
			entity.setUserId(item.getUserId());
			entity.setThirdUserId(item.getThirdUserId());
			entity.setAnonyToken(item.getAnonymousToken());
			entity.setReferralCode(item.getReferralCode());
			entity.setReferralUserId(item.getReferralUserId());
			entity.setRelIdType(item.getRelIdType());
			entity.setRelId(item.getRelId());
			entity.setCman(item.getUserId());
			entity.setCdt(LocalDateTime.now());
			entity.setCip(item.getIp());
			userBrowseObjList.add(entity);
		});
		
		if (userBrowseObjList.size() > 0) {
			userBrowseObjLogService.saveBatch(userBrowseObjList);
		}
	}

	@Override
	public void saveUserBrowseObjLogOne(UserBrowseObjLogAddRequest params) {
		UserBrowseObjLog entity=new UserBrowseObjLog();
		entity.setCompanyNo(params.getCompanyNo());
		entity.setUserId(params.getUserId());
		entity.setThirdUserId(params.getThirdUserId());
		entity.setAnonyToken(params.getAnonymousToken());
		entity.setReferralCode(params.getReferralCode());
		entity.setReferralUserId(params.getReferralUserId());
		entity.setRelIdType(params.getRelIdType());
		entity.setRelId(params.getRelId());
		entity.setCman(params.getUserId());
		entity.setCdt(LocalDateTime.now());
		entity.setCip(params.getIp());
		userBrowseObjLogService.save(entity);
	}

	@Override
	public void saveRel(Integer custUserId,Integer userId,String ip){
		Contact2UserAddRequest request=new Contact2UserAddRequest();
		request.setCustUserId(custUserId);
		request.setUserId(userId);
		request.setIp(ip);
		crmRemoteService.addContact2User(request);
	}


}
